package com.chuanye.mydrawable.adapters;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanye.mydrawable.R;
import com.chuanye.mydrawable.activitys.RemoteActivity;
import com.chuanye.mydrawable.beans.UserBean;

import java.util.HashMap;
import java.util.List;

/**
 * Created by lsef on 2019/2/15.
 */

public class PersonnelAdapter extends BaseAdapter{
    private Context mContext;
    private List<UserBean.ListBean> userBeanList;
    private List<UserBean.ListBean> userBeanList2;
    // 用来控制CheckBox的选中状况
    private static HashMap<Integer, Boolean> isSelected;

    public PersonnelAdapter(Context mContext, List<UserBean.ListBean> userBeanList,HashMap<Integer, Boolean> isSelected,List<UserBean.ListBean> userBeanList2) {
        this.mContext = mContext;
        this.userBeanList = userBeanList;

        this.isSelected = isSelected;
        this.userBeanList2 = userBeanList2;
        // 初始化数据
        initDate();

    }

    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < userBeanList.size(); i++) {
            getIsSelected().put(i, false);
        }
    }

    @Override
    public int getCount() {
        return userBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return userBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null){
            view = View.inflate(mContext, R.layout.support_itme,null);
            holder = new ViewHolder();
            holder.support_item_tv = view.findViewById(R.id.support_item_tv);
            holder.support_item_iv = view.findViewById(R.id.support_item_iv);
            holder.support_item_ll = view.findViewById(R.id.support_item_ll);
            view.setTag(holder);
        }
        holder = (ViewHolder) view.getTag();
        holder.support_item_tv.setText(userBeanList.get(i).getUserName());

        if (userBeanList2 != null && userBeanList2.size() > 0){
            for (int f = 0; f < userBeanList2.size(); f ++){
                if (userBeanList2.get(f).getUserName().equals(userBeanList.get(i).getUserName())){
                    isSelected.put(i, true);
                }else {
                    //isSelected.put(i, false);
                }
            }
        }

        // 监听checkBox并根据原来的状态来设置新的状态
        holder.support_item_ll.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                if (isSelected.get(i)) {
                    isSelected.put(i, false);
                    setIsSelected(isSelected);
                    if (RemoteActivity.userlist2 !=null && RemoteActivity.userlist2.size() > 0){
                        for(int k = 0 ; k < RemoteActivity.userlist2.size(); k++){
                            if (RemoteActivity.userlist2.get(k).getUserName().equals(userBeanList.get(i).getUserName())){
                                RemoteActivity.userlist2.remove(k);
                                RemoteActivity.recyPersonnelAdapter.notifyDataSetChanged();
                            }
                        }
                    }


                } else {
                    isSelected.put(i, true);
                    setIsSelected(isSelected);
                    RemoteActivity.userlist2.add(userBeanList.get(i));
                    RemoteActivity.recyPersonnelAdapter.notifyDataSetChanged();
                    RemoteActivity.crreremot_man_tv.setVisibility(View.GONE);
                    RemoteActivity.crreremot_man_rv.setVisibility(View.VISIBLE);
                }
                notifyDataSetChanged();
            }
        });
        // 根据isSelected来设置checkbox的选中状况
        if (getIsSelected().get(i)){
            holder.support_item_iv.setVisibility(View.VISIBLE);
        }else {
            holder.support_item_iv.setVisibility(View.GONE);
        }

        return view;
    }

    public static class ViewHolder{
        TextView support_item_tv;
        ImageView support_item_iv;
        LinearLayout support_item_ll;
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        PersonnelAdapter.isSelected = isSelected;
    }
}
